package com.sxt;

import javax.swing.*;
import java.awt.*;

//定义粉线类
public class Line {
    //起点坐标
    int x=530;
    int y=100;
    //终点坐标
    int endx=500;
    int endy=500;

    //线长
    double length=50;
    //限定线长
    double MIN_length=50;
    double MAX_length=570;
    //线角度
    double angle=0;
    //限定方向范围
    int dir = 1;
    //粉线抓取 0 摇摆 1 抓取 2 收回 3 抓取返回
    int state; //定义状态

    //钩爪图片
    Image hook = Toolkit.getDefaultToolkit().getImage("image/hook.png");

    //定义抓取判定
    GameWin frame;
    //定义一个有参构造
    Line(GameWin frame) {
        this.frame = frame;
    }
    //碰撞检测检测金块石块是否被钩爪勾取
    void logic(){
        //因为GameWin中金块使用增强for循环中变量为obj因为不能找到gold，此处调用obj
        for(Object obj:this.frame.objectList){
            if(endx>obj.x && endx<obj.x+obj.width
                    && endy>obj.y && endy<obj.y+obj.height){
                state = 3;
                obj.flag = true;
        }

        }
    }
    //定义绘制方法，防止代码重复过高
    void lines(Graphics g) {
        endx = (int) (x + length*Math.cos(angle * Math.PI));
        endy = (int) (y + length*Math.sin(angle * Math.PI));
        //定义颜色
        g.setColor(Color.pink);
        //绘制线
        g.drawLine(x,y,endx,endy);
        //将线加粗(多绘制两条平行线）
        g.drawLine(x-1,y,endx-1,endy);
        g.drawLine(x+1,y,endx+1,endy);
        //绘制图片
        g.drawImage(hook,endx-32,endy-2,null);
    }

    //绘制方法
    void paintself(Graphics g) {
        logic();
        //鼠标点击状态
        switch (state) {
            case 0://摇摆
                //限定线摇摆的范围
                if (angle<0.05){dir=1;}
                else if (angle>0.95){dir=-1;}
                //线摇摆
                angle = angle + 0.005*dir;
                lines(g);
                break;
            case 1://抓取
                if(length<MAX_length){
                    length = length + 5;
                    lines(g);
                }else{state = 2;}
                break;
            case 2://收回
                if(length>=MIN_length){
                    length = length - 5;
                    lines(g);
                }else{state = 0;}
                break;
            case 3://和2类似，也是返回，但是触发条件不同
                //设置初始质量
                int m =1;
                if(length>=MIN_length){
                    length = length - 5;
                    lines(g);
                    for(Object obj:this.frame.objectList){
                        //先判断能否移动再将其移动
                        if(obj.flag){
                            //获取物体质量
                            m = obj.m;
                            //写金块状态
                            obj.x = endx-obj.getWidth()/2;
                            obj.y = endy;
                            if(length<=MIN_length){
                                //抓取成功时直接移除金块(直接移除到屏幕外）
                                obj.x = -500;
                                obj.y = 1000;
                                obj.flag = false;
                                Bg.waterFlag = false;
                                //加分
                                Bg.count += obj.count;
                                state = 0;}
                            //如果waterFlag此刻状态为True
                            if (Bg.waterFlag){
                                if(obj.type == 1){
                                    m = 1;
                                }
                                if (obj.type == 2){
                                    obj.x = -500;
                                    obj.y = 1000;
                                    obj.flag = false;
                                    //药水变为不可用
                                    Bg.waterFlag = false;
                                    state = 2;
                                }
                            }
                        }

                    }
                }
                //质量越大拉取越慢，在此处添加一个延时造成缓慢的效果
                try {
                    Thread.sleep(m);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                break;
                default:
        }

    }
}
